<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" type="text/css" href="../themes/default/easyui.css">
	<link rel="stylesheet" type="text/css" href="../themes/icon.css">
	<script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="../js/jquery.serializejson.min.js"></script>
    
    
    <title>Document</title>
</head>
<body>
    
    <table id="dg"></table>
    <div id="dlg" class="easyui-dialog" title="Basic Dialog" closed="true" data-options="iconCls:'icon-save'" style="width:500px;height:250px;padding:10px">
        <form id="ff" method="post">
            <div style="margin-bottom:20px;display:none" >
                <input class="easyui-textbox" name="_id" style="width:100%" data-options="label:'id:',required:true">
            </div>
            <div style="margin-bottom:20px">
                <input class="easyui-textbox" name="email" style="width:100%" data-options="label:'email:',required:true">
            </div>
            <div style="margin-bottom:20px">
                    <input class="easyui-textbox" name="password" style="width:100%" data-options="label:'password:',required:true">
                </div>
            <a href="#" id="dataSumbit" class="easyui-linkbutton" data-options="iconCls:'icon-add'">提交</a>
        </form>
    </div>
    <div style="padding:5px 0;" id="ft">
        用户搜索：
        <input class="easyui-input" style="width:110px;height:20px" name="email" id="key">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search'" style="width:80px" onclick=doSearch()>Search</a>
    </div>
</body>
</html>
<script> 
    var searchresult = null;  
    function doSearch(){
        var key = $('#key').val();
        $.ajax({
            url:`http://localhost:3000/users/list`,
            type:'post',
            data:{
                email:key
            }
        }).then(res=>{
            if(res){
                $('#dg').datagrid('reload',{email:key});
            }
        });
    }
    $('#dg').datagrid({
        loadMsg: '加载中,请稍等',
        url:'http://localhost:3000/users/list',
        fit:true,
        pagination:true,
        pageSize:5,
        pageList:[5,10,20,30,40,50],
        columns:[[
            {field:'ck',checkbox:true} ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
            {field:'email',title:'email',width:100,align:'center'} ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
            {field:'operate',title:'操作',width:200,align:'center',
                formatter: function(value,row,index){
                   return "<a href='javascript:void(0)' onclick=onDelete('"+row._id+"')>删除</a> <a href='javascript:void(0)' onclick=onUpdata('"+row._id+"')>修改</a>"
                }
            }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
        ]],
        toolbar: [{
            iconCls: 'icon-add',
            handler: function(){
                $('#ff').form('clear');
                $('#dlg').dialog('open');
            }
        },'-',{
            iconCls: 'icon-remove',
            handler: function(){
                var ids = [];
                var rows = $('#dg').datagrid('getSelections');
                if(rows.length>0){
                    for(var i in rows){
                        ids.push(rows[i]._id);
                    }
                }
                $.messager.confirm('记录删除', '确定要删除吗?', function(r){
                    if (r){
                        $.ajax({
                            url:`http://localhost:3000/users/data/removes`,
                            type:'post',
                            data:{
                                ids:ids.toString()
                            }
                        }).then(res=>{
                            if(res.status==200){
                                $.messager.show({
                                    title:'信息提示',
                                    msg:res.message,
                                    showType:'show'
                                });
                                $('#dg').datagrid('reload');
                            }
                            
                        });
                    }
                });
            }
        },'-',{
            iconCls: 'icon-edit',
            handler: function(){alert('edit')}
        },'-',{
            iconCls: 'icon-help',
            handler: function(){alert('help')}
        }],
        footer:'#ft'
    });
    $('#dataSumbit').click(function (){ 
        var formData = $('#ff').serializeJSON();
        if(formData._id.trim().length>0){
            $.ajax({
                url:`http://localhost:3000/users/data/${formData._id}`,
                type:'put',
                data:formData
            }).then(function (res) { 
                $('#dlg').dialog('close');
                $('#dg').datagrid('reload');
            });

        }else{
            delete formData._id;
            $.ajax({
                url:'http://localhost:3000/users/data',
                type:'post',
                data:formData
            }).then(function (res) { 
                $('#dlg').dialog('close');
                $('#dg').datagrid('reload');
            })
        }
     });

     function onDelete(id){
        $.messager.confirm('记录删除', '确定要删除吗?', function(r){
            if (r){
                $.ajax({
                    url:`http://localhost:3000/users/data/${id}`,
                    type:'delete'
                }).then(res=>{
                    if(res.status==200){
                        $.messager.show({
                            title:'信息提示',
                            msg:res.message,
                            showType:'show'
                        });
                        $('#dg').datagrid('reload');
                    }
                    
                });
            }
        });
        
     }

    function onUpdata(id) {
        $.ajax({
            url:`http://localhost:3000/users/data/${id}`,
            type:'get',
        }).then(function (res) { 
            $('#ff').form('load',res);
            $('#dlg').dialog('open');
        })
    }
</script>